//
//  main.cpp
//  FirstOpenCV
//
//  Created by Y S on 2018/12/20.
//  Copyright © 2018年 Y S. All rights reserved.
//

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

int main(int argc, const char * argv[]) {
    String root = "/Users/ys/Documents/csdn/code/03_week_homework_advance/";
    
    String src_path = root + "lena.jpg";
    
    String bgr_path = root + "lena_bgr.jpg";
    String hsv_path = root + "lena_hsv.jpg";
    String gray_path = root + "lena_gray.jpg";
    
    String b_path = root + "lena_b.jpg";
    String g_path = root + "lena_g.jpg";
    String r_path = root + "lena_r.jpg";
    
    String h_path = root + "lena_h.jpg";
    String s_path = root + "lena_s.jpg";
    String v_path = root + "lena_v.jpg";
    
    Mat bgr, gray, hsv, hsvChannels[3];
    std::vector<Mat> bgrChannels;
    
    Mat src = imread(src_path);
    imshow("Hello, Hello!", src);//显示原始图像
    
    /*
     * 降采样
     */
    pyrDown(src, bgr);
    imshow("bgr", bgr);//显示压缩的彩色图像
    imwrite(bgr_path, bgr);
    
    /*
     * 灰度转换
     */
    cvtColor(bgr, gray, COLOR_RGB2GRAY);
    imshow("gray", gray);//显示灰度图像
    imwrite(gray_path, gray);
    
    /*
     * 颜色通道合成
     */
    Mat dst;
    split(bgr, bgrChannels);
    merge(bgrChannels, dst);
    
    //用彩色图像形象的表示一下,除了原通道保留，其余两通道置0
    Mat tmp(bgr.size(),CV_8U,Scalar(0));
    std::vector<Mat> b,g,r;   //用来表示的彩色图像
    for(int i=0;i<3;i++)
    {
        if(i==0)
            b.push_back(bgrChannels[0]);
        else
            b.push_back(tmp);
        
        if(i==1)
            g.push_back(bgrChannels[1]);
        else
            g.push_back(tmp);
        
        if(i==2)
            r.push_back(bgrChannels[2]);
        else
            r.push_back(tmp);
    }
    
    merge(b,dst);
    imshow("b",dst);
    imwrite(b_path, dst);
    
    merge(g,dst);
    imshow("g",dst);
    imwrite(g_path, dst);
    
    merge(r,dst);
    imshow("r",dst);
    imwrite(r_path, dst);
    
    /*
     * RGB转换成HSV
     */
    cvtColor(bgr, hsv, COLOR_BGR2HSV);
    imshow("HSV", hsv);
    imwrite(hsv_path, hsv);
    
    /*
     * 通道分离
     */
    split(hsv, hsvChannels);
    
    /*
     * 色调H，用角度度量，
     * 取值范围为0°～360°，红色为0°，绿色为120°,蓝色为240°
     */
    imshow("Hue", hsvChannels[0]);
    imwrite(h_path, hsvChannels[0]);
    
    /*
     * 饱和度S表示颜色接近光谱色的程度
     * 饱和度高，颜色则深而艳
     * 光谱色的白光成分为0，饱和度达到最高
     * 通常取值范围为0%～100%，值越大，颜色越饱和
     */
    imshow("Saturation", hsvChannels[1]);
    imwrite(s_path, hsvChannels[1]);
    
    /*
     * 明度V，明度表示颜色明亮的程度，
     * 对于光源色，明度值与发光体的光亮度有关；对于物体色，此值和物体的透射比或反射比有关。
     * 通常取值范围为0%（黑）到100%（白）
     */
    imshow("Value", hsvChannels[2]);
    imwrite(v_path, hsvChannels[2]);
    
    IplImage *rgbImage=cvLoadImage(bgr_path.c_str(), 1);
    IplImage *hsvImage=cvLoadImage(hsv_path.c_str(), 1);
    
    CvScalar s;
    for(int y=0; y<rgbImage->height; y++)
    {
        for(int x=0; x<rgbImage->width; x++)
        {
            /*
             * 输出RGB图像每个像素的BGR值
             */
            s=cvGet2D(rgbImage,y,x); //图像中坐标为（i,j）的像素点的值
            printf("x=%d,y=%d, BGR: (%0.2f, %0.2f, %0.2f) \n",x,y,s.val[0],s.val[1],s.val[2]);
            
            /*
             * 输出HSV图像的每个像素的HSV值
             */
            s = cvGet2D(hsvImage, y, x); //图像中坐标为（i,j）的像素点的值
            printf("x=%d,y=%d, HSV: (%0.2f, %0.2f, %0.2f) \n",x,y,s.val[0],s.val[1],s.val[2]);
        }
    }
    
    waitKey();
    
    return 0;
}
